package com.suthund.frame.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/***
 * Created by 杨康 on 2017/4/18.
 */
@Aspect
@Component
public class ApiControllerAspect {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Pointcut(" execution(* com.suthund.controller.ManageController.*(..))")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void log(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //统一处理请求日志
        log.info("url={}", request.getRequestURI());
        log.info("requestType={}", request.getMethod());
        log.info("ip={}", request.getRemoteAddr());
        log.info("classMethod={}", joinPoint.getSignature().getDeclaringTypeName());
        log.info("MethodName={}", joinPoint.getSignature().getName());
        log.info("args={}", joinPoint.getArgs());
    }

    /**
     * 统以处理返回数据日志
     */
    @AfterReturning(returning = "object", pointcut = "pointCut()")
    public void doAfter(Object object) {
        log.info("response={}", object.toString());
    }
}
